package com.cn.sl.server.api.system.user.controller

import cn.dev33.satoken.annotation.SaCheckLogin
import com.cn.sl.server.api.system.menu.model.dto.MenuDto
import com.cn.sl.server.api.system.user.model.dto.AddAdminUserParam
import com.cn.sl.server.api.system.user.model.dto.AdminUserAppendRoleParam
import com.cn.sl.server.api.system.user.model.dto.AdminUserDto
import com.cn.sl.server.api.system.user.model.dto.AdminUserListQueryDto
import com.cn.sl.server.api.system.user.model.dto.EditAdminUserParam
import com.cn.sl.server.api.system.user.model.dto.UpdateAdminUserStatusParam
import com.cn.sl.server.api.system.user.model.dto.UserPhoneExistParam
import com.cn.sl.server.api.system.user.service.AdminUserService
import com.cn.sl.server.common.constant.Constant
import com.cn.sl.server.common.dto.PageDto
import com.cn.sl.server.common.dto.SingleValue
import com.cn.sl.server.util.ResponseMessageDto
import com.cn.sl.server.util.deleteSuccessMsg
import com.cn.sl.server.util.ok
import com.cn.sl.server.util.requestSuccessMsg
import com.cn.sl.server.util.saveSuccessMsg
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import org.springframework.http.ResponseEntity
import org.springframework.validation.annotation.Validated
import org.springframework.web.bind.annotation.DeleteMapping
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

/**
 * 登录相关
 *
 * @author 阮雪峰
 */
@Tag(name = "用户管理")
@SaCheckLogin
@RestController
@RequestMapping("${Constant.ADMIN_ROOT}/admin-user")
class AdminUserController(
    val adminUserService: AdminUserService
) {

    /**
     * 列表查询所有
     *
     * @param dto dto
     * @return org.springframework.http.ResponseEntity
     * @author 阮雪峰
     */
    @Operation(summary = "列表查询所有")
    @PostMapping("/find-all")
    fun findAll(@RequestBody dto: AdminUserListQueryDto): ResponseEntity<List<AdminUserDto>> {
        return ok(adminUserService.findAll(dto))
    }

    /**
     * 列表查询
     *
     * @param dto dto
     * @return org.springframework.http.ResponseEntity
     * @author 阮雪峰
     */
    @Operation(summary = "列表查询")
    @PostMapping("/find")
    fun find(@RequestBody dto: AdminUserListQueryDto): ResponseEntity<PageDto<AdminUserDto>> {
        return ok(adminUserService.find(dto))
    }

    /**
     * 列表查询
     *
     * @param id id
     * @return org.springframework.http.ResponseEntity
     * @author 阮雪峰
     */
    @Operation(summary = "查询用户拥有的菜单")
    @PostMapping("/menu/{id}")
    fun menu(@PathVariable id: Long): ResponseEntity<List<MenuDto>> {
        return ok(adminUserService.findMenu(id))
    }

    /**
     * 电话是否存在
     *
     * @param param param
     * @return org.springframework.http.ResponseEntity
     * @author 阮雪峰
     */
    @Operation(summary = "电话是否存在")
    @PostMapping("/phone-exist")
    fun phoneExist(@RequestBody @Validated param: UserPhoneExistParam): ResponseEntity<SingleValue<Boolean>> {
        return ok(SingleValue(adminUserService.phoneExists(param.toDto())))
    }

    /**
     * 新增
     *
     * @param param param
     * @return org.springframework.http.ResponseEntity
     * @author 阮雪峰
     */
    @Operation(summary = "新增")
    @PostMapping("/add")
    fun add(@RequestBody @Validated param: AddAdminUserParam): ResponseEntity<ResponseMessageDto> {
        adminUserService.add(param.toDto())
        return saveSuccessMsg()
    }

    /**
     * 编辑
     *
     * @param param param
     * @return org.springframework.http.ResponseEntity
     * @author 阮雪峰
     */
    @Operation(summary = "编辑")
    @PostMapping("/edit")
    fun edit(@RequestBody @Validated param: EditAdminUserParam): ResponseEntity<ResponseMessageDto> {
        adminUserService.edit(param.toDto())
        return saveSuccessMsg()
    }

    /**
     * 根据id查询
     *
     * @param id id
     * @return org.springframework.http.ResponseEntity
     * @author 阮雪峰
     */
    @Operation(summary = "根据id查询")
    @GetMapping("/find/{id}")
    fun findById(@PathVariable id: Long): ResponseEntity<AdminUserDto> {
        return ok(adminUserService.findById(id))
    }

    /**
     * 根据id删除
     *
     * @param id id
     * @return org.springframework.http.ResponseEntity
     * @author 阮雪峰
     */
    @Operation(summary = "根据id删除")
    @DeleteMapping("/delete/{id}")
    fun delete(@PathVariable id: Long): ResponseEntity<ResponseMessageDto> {
        adminUserService.delete(id)
        return deleteSuccessMsg()
    }

    /**
     * 更新用户状态
     *
     * @param param param
     * @return org.springframework.http.ResponseEntity
     * @author 阮雪峰
     */
    @Operation(summary = "更新用户状态")
    @PostMapping("/update-status")
    fun updateStatus(@RequestBody @Validated param: UpdateAdminUserStatusParam): ResponseEntity<ResponseMessageDto> {
        adminUserService.updateStatus(param.toDto())
        return saveSuccessMsg()
    }

    /**
     * 追加角色
     *
     * @param param param
     * @return org.springframework.http.ResponseEntity
     * @author 阮雪峰
     */
    @Operation(summary = "追加角色")
    @PostMapping("/append-role")
    fun appendRole(@RequestBody @Validated param: AdminUserAppendRoleParam): ResponseEntity<ResponseMessageDto> {
        adminUserService.appendRole(param.toDto())
        return saveSuccessMsg()
    }

    /**
     * 重置密码
     *
     * @param id id
     * @return org.springframework.http.ResponseEntity
     * @author 阮雪峰
     */
    @Operation(summary = "重置密码")
    @PostMapping("/reset/password/{id}")
    fun resetPassword(@PathVariable id: Long): ResponseEntity<ResponseMessageDto> {
        adminUserService.resetPassword(id)
        return requestSuccessMsg()
    }
}